//
//  WebServiceDataConverter.m
//  eGiftr
//
//  Created by Phien Tram on 9/4/13.
//  Copyright (c) 2013 eGiftr. All rights reserved.
//

#import "WebServiceDataConverter.h"
#import "WishTerm.h"
#import "FindKeyword.h"
#import "FriendInfo.h"
#import "FriendsByUserIdResult.h"
#import "NSDictionary+Utility.h"
#import "Utils.h"

@implementation WebServiceDataConverter

+ (WishTerm *)convertJsonDataToMyWishList:(NSDictionary *)data
{
    WishTerm *term = [[WishTerm alloc] init];
    term.identifier = [data objectOrNilForKey:@"id"];
    term.createdDate = [data objectOrNilForKey:@"createdDate"];
    term.keyword = [data objectOrNilForKey:@"keyword"];
    term.userId = [data objectOrNilForKey:@"userId"];
    term.cate = [data objectOrNilForKey:@"cate"];
    term.subCate = [data objectOrNilForKey:@"subCate"];
    
    
    return term;
}

/*
 address = "<null>";
 birthday = "/Date(1376991545000-0400)/";
 city = "<null>";
 email = "<null>";
 fbId = 100004789414858;
 firstName = Tr;
 gender = 1;
 id = "330fa8aa-ce34-27ce-1eff-08d07b2e684f";
 isActive = 0;
 lastName = Ungnguyen;
 latitude = "<null>";
 locale = "en_US";
 longitude = "<null>";
 phone = "<null>";
 relationshipStatus = "<null>";
 state = "<null>";
 updatedDate = "/Date(1378716270970-0400)/";
 username = "";
 zipCode = "<null>";
 */

+ (FriendsByUserIdResult *)convertJsonDataToFriendList:(NSDictionary *)data
{
    NSMutableArray *mutableArray = [NSMutableArray array];
    
    FriendsByUserIdResult *result = [[FriendsByUserIdResult alloc] init];
    result.currentPage = [[data objectOrNilForKey:@"currentPage"] integerValue];
    result.totalPages = [[data objectOrNilForKey:@"totalPages"] integerValue];
    result.totalRecords = [[data objectOrNilForKey:@"totalRecords"] integerValue];
    
    NSArray *results = [data objectOrNilForKey:@"results"];
    for (NSDictionary *result in results) {
        FriendInfo *friend = [[FriendInfo alloc] init];
        friend.address = [result objectOrNilForKey:@"address"];
        NSString *birthday = [result objectOrNilForKey:@"birthday"];
        if (birthday) {
            friend.birthday = [Utils mfDateFromDotNetJSONString:birthday];
        }
        friend.city = [result objectOrNilForKey:@"city"];
        friend.email = [result objectOrNilForKey:@"email"];
        friend.fbId = [result objectOrNilForKey:@"fbId"];
        friend.firstName = [result objectOrNilForKey:@"firstName"];
        friend.gender = [[result objectOrNilForKey:@"gender"] integerValue];
        friend.identifier = [result objectOrNilForKey:@"id"];
        friend.isActive = [[result objectOrNilForKey:@"isActive"] boolValue];
        friend.lastName = [result objectOrNilForKey:@"lastName"];
        friend.latitude = [result objectOrNilForKey:@"latitude"];
        friend.locale = [result objectOrNilForKey:@"locale"];
        friend.longitude = [result objectOrNilForKey:@"longitude"];
        friend.phone = [result objectOrNilForKey:@"phone"];
        friend.profileImageUrl = [result objectOrNilForKey:@"profileImageUrl"];
        friend.relationshipStatus = [result objectOrNilForKey:@"relationshipStatus"];
        friend.state = [result objectOrNilForKey:@"state"];
        friend.updatedDate = [result objectOrNilForKey:@"updatedDate"];
        friend.username = [result objectOrNilForKey:@"username"];
        friend.zipCode = [result objectOrNilForKey:@"zipCode"];
        
        [mutableArray addObject:friend];
    }
    result.results = [NSArray arrayWithArray:mutableArray];
    
    return result;
}

+ (NSArray *)convertJsonDataToFindKeywords:(NSDictionary *)data
{
    NSMutableArray *mutableArray = [NSMutableArray array];
    
    NSArray *results = [data objectOrNilForKey:@"results"];
    for (NSDictionary *result in results) {
        FindKeyword *findKeyword = [[FindKeyword alloc] init];
        findKeyword.identifier = [result objectOrNilForKey:@"id"];
        findKeyword.cateName = [result objectOrNilForKey:@"cateName"];
        findKeyword.subCateName = [result objectOrNilForKey:@"subCateName"];
        findKeyword.keyword = [result objectOrNilForKey:@"keyword"];
        
        [mutableArray addObject:findKeyword];
    }
    
    return [NSArray arrayWithArray:mutableArray];
}

@end
